﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eff.Extensions;
using Redmine.Core.Libs.Redmine;
using Redmine.Core.Repositories;
using Redmine.Core.UnitOfWork;

namespace Redmine.Web.Helpers
{
    public class ApplicationHelper
    {
        public const string Favicon = "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />";

        public dynamic GlobalSetting;
        private readonly ISettingRepository _settingRepository;
        public ApplicationHelper()
        {
            _settingRepository = new SettingRepository(new MyDatabaseFactory());
        }
        public ApplicationHelper(ISettingRepository settingRepository)
        {
            _settingRepository = settingRepository;
        }

        //        public static void ApplicationInit()
        //        {
        //            ISettingRepository settingRepository = new SettingRepository(new MyDatabaseFactory());
        //            var theme = settingRepository.Find(t => t.Name.Equals("ui_theme")).FirstOrDefault();
        //            GlobalSetting.Theme = string.Format("'~/Content{0}/stylesheets/application.css'", theme != null ? "/theme/" + theme : "");
        //        }

        public string Theme
        {
            get
            {
                var theme = _settingRepository["ui_theme"];
                return string.Format("'~/Content{0}/stylesheets/application.css'", theme != null ? "/theme/" + theme : "");
            }
        }

        public string BodyCssClasses
        {
            get
            {
                var css = new List<string>(3);
                Theme theme = ThemeModule.GetTheme(_settingRepository["ui_theme"]);
                if (theme != null)
                    css.Add("theme-" + theme.DirName);
                var requestContext = HttpContext.Current.Request.RequestContext;
                css.Add("controller-" + requestContext.RouteData.Values["controller"]);
                css.Add("action-" + requestContext.RouteData.Values["action"]);
                return css.ConcatEnumerable(" ");
            }
        }
    }
}